/**
 * Created by Administrator on 2017/6/7 0007.
 */

var express = require('express');
var router = express.Router();
var multiparty = require('multiparty');
var DB = require('../../model/db');
var sd = require('silly-datetime');
var async = require("async");
var md5 = require("md5-node");
var session = require("express-session");  /*����session*/
var Host = "";
router.use(function (req, res, next) {
    Host = "http://" + req.headers.host;
    next();
})

router.get('/',function(req,res){
   var page=req.query.page;
    var pageSize=4
    if(!page){
        page=1
    }
   async.parallel({
    count:function(callback){
        DB.count('admin',{},function(err,data){
           callback(err,data)
        })
    },
    user:function(callback){
        DB.find('admin',{},{pageSize:pageSize,page:page},function(err,data){
            callback(err,data)
        })
    }
   },function(err,result){
           // console.log(result)
        res.render('admin/admin/index',{
              host:Host,
              list:result.user,
              page:page,
              totalPage:Math.ceil(result.count/pageSize)
        })
   })

})
router.get('/add',function(req,res,next){

     res.render('admin/admin/add',{
         host:Host
     })

})
router.post('/addPost',function(req,res){
    var form = new multiparty.Form();
    form.uploadDir = "./public/resource";
 //   console.log(form.uploadDir);
    form.parse(req,function(err,fields,files){

        var username = fields.username[0];
        var password = md5(fields.password[0]);
        var photo = files.photo[0].path;
        var status = fields.status[0];
        var addtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
        DB.insertOne('admin',{
            username:username,
            password:password,
            img:photo,
            status:status,
            add_time:addtime
        },function(err,result){
            if(err){
                console.log(err);
                return;
            }
          res.send('<script>alert("增加成功");location.href="/admin/register"</script>')
        })
    })
})
router.get('/edit/:id',function(req,res,next){
     var id=req.params.id;
    DB.find('admin',{'_id':new DB.ObjectID(id)},function(err,data){
        if(err){
            console.log(err);
            return;
        }
       res.render('admin/admin/edit',{
           host:Host,
           list:data
       })
    })
})
router.post('/editPost',function(req,res){
    var form = new multiparty.Form();
    form.uploadDir = "./public/resource";

    form.parse(req,function(err,fields,files){
        var id=fields.id[0];
        var username = fields.username[0];
        var password = md5(fields.password[0]);
        var photo = files.photo[0].path;
        var status = fields.status[0];
        var addtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
        var originalFilename = files.photo[0].originalFilename;
        if(originalFilename){
            console.log(1)
            var setData={
                username:username,
                password:password,
                img:photo,
                status:status,
                add_time:addtime
            }
        }else{
            console.log(2)
            var setData={
                username:username,
                password:password,
                status:status,
                add_time:addtime
            }
        }
        DB.updateOne('admin',{_id:new DB.ObjectID(id)},setData,function(err,result){
            if(err){
                console.log(err);
                return;
            }
            res.send('<script>alert("修改成功");location.href="/admin/register"</script>')
        })
    })
})
router.get('/delete/:id',function(req,res){
      var id=req.params.id;
    DB.deleteMany('admin',{"_id":new DB.ObjectID(id)},function(err,result){
        if(err){
            console.log(err);
            return
        }
        res.redirect(Host + "/admin/register");
    })
})
module.exports = router;

